from state import *

class StateParser(object):
	def __init__(self,env):
		self.env = env
		self.names = dict()
		self.constraints = []

	def space(self):
		return StateSpace(set([n for n in self.names]),self.constraints)
	def initial(self):
		return self.names
	
	def parse(self, code, start):
		for line in code:
			if len(line.strip())==0: continue
			# expect one assignment per line
			lineParts = [l.strip() for l in line.split('=')]
			if len(lineParts) != 2:
				raise RuntimeError("STRIPS: Expected one assignment per line: "\
									+ line)
			varName, varVal = lineParts
			if varName in self.env or varName in self.names:
				raise RuntimeError("STRIPS: Name collision: " + varName)
			self.names[varName] = eval(varVal, self.env)

	def parse_constraints(self, code, start):
		for line in code:
			line=line.strip()
			if len(line) > 0:
				self.constraints.append(line)
